import pandas as pd
import numpy as np
from scipy.interpolate import lagrange
# def ployinterp_column(s,n,k=1):
#     y=s.reindex(list(range(n-k,n))+list(range(n+1,n+1+k)))
#     y=y[y.notnull()]
#     return  int(abs(lagrange(y.index,list(y))(n)))
#s是原始数据，n是插补位置（索引），k为前后数据个数
def ployinterp_column(s,n,k=1):
    y1 = list(range(n-k,n))
    y2 = list(range(n+1,n+k+1))
    y = s.reindex(y1+y2)
    y = y[y.notnull()]
    ln = lagrange(y.index, list(y))
    x = ln(n)
    return int(abs(x))

    # if len(y) <2:
    #     print('前后非空数据长度小于二，无法使用拉格朗日方法')
    #     return np.nan
    # else:
        # 根据数据拉格朗日

#
# data = pd.read_excel(".\data3\computer_3_old_short.xlsx")
# print('原本数据：')
# print(data.info())
# for i in data.columns:
#     for j in range(len(data)):
#         if data.at[j,i] is None or pd.isnull(data.at[j,i]):
#             data.at[j,i] = ployinterp_column(data[i],j,1)
# data.to_excel(".\data3\computer_3_new_short.xlsx")


data1 = pd.read_excel(".\data3\computer_3_old_long.xlsx")
print('原本数据：')
print(data1.info())
for i in data1.columns:
    for j in range(len(data1)):
        if data1.at[j,i] is None or pd.isnull(data1.at[j,i]):
            data1.at[j,i] = ployinterp_column(data1[i],j,1)
# data1=data1.drop('Unnamed:0',axis = 1)
data1.to_excel(".\data3\computer_3_new_long.xlsx")
print("处理后数据：")
print(data1.info())